<template>
  <div>
    <div
      style="position:fixed; top: 45px;bottom: 70px;width:100%"
      :on-refresh="refresh"
      no-data-text="暂无更多数据"
      loading-layer-color="position: relative; top: -0.9em; padding: 0 .55em; color: #999999;font-size:5em"
      refresh-layer-color="#4b8bf4"
      :on-infinite="infinite"
    >
      <TreeRow
        :list.sync="list"
        style="position:absolute;"
        :view-id.sync="viewId"
        :show-operation="true"
        :model.sync="model"
        :header="model.name"
        @error-toast="errorToast"
        @on-click-tree-item="treeRowClick"
      />
    </div>
    <!--<toast-->
    <!--v-model="toastShow"-->
    <!--:type="toastType"-->
    <!--&gt;-->
    <!--{{ toastMsg }}-->
    <!--</toast>-->
  </div>
</template>

<script>
  import TreeRow from '../ctrls/TreeRow.vue'
  import {mapState} from 'vuex'

  export default {
  name: 'Tree',
  components: {
    // PullRefresh,
    // Panel,
    TreeRow
  },
  props: {
    model: { type: String, default: '' },
    domain: { type: Array, default () { return [] } },
    viewId: { type: Number, default: 1 },
    limit: { type: Number, default: 10 },
    offsetStep: { type: Number, default: 10 },
    order: { type: String, default: '' }
  },
  data () {
    return {
      offset: 0,
      toastShow: false,
      toastType: '',
      toastMsg: '',
      is_all_records_data: false,
      now_record_length: this.offset_step,
      list: []
    }
  },
  computed: {
    ...mapState({
      route: state => state.route,
      path: state => state.route.path,
      isLoading: state => state.app_root.isLoading,
      app_root: state => state.app_root
    })
  },
  watch: {
    domain: {
      handler: function (val, oldVal) {
        let self = this
        self.offset = 0
        self.get_more_data(self.offset, 'fresh')
      }
    }
  },
    mounted: function () {
    let self = this
    self.app_root.menus = ['新建', '取消']
    self.app_root.actionSheetFunction = self.actionSheetFunction
    this.$nextTick(() => {
      self.offset = 0
      self.get_more_data(self.offset, 'refresh')
    })
    self.get_more_data(self.offset, 'refresh')
  },
  methods: {
    change: function (status) {
    },
    fresh: function () {
      this.$nextTick(() => {
        self.offset = 0
        self.list = []
        self.get_more_data(0, 'refresh')
      })
    },
    errorToast: function (message) {
      let self = this
      self.toastShow = message.toastShow
      self.toastType = message.toastType
      self.toastMsg = message.toastMsg
    },
    treeRowClick: function (item) {
      this.$emit('on-click-item', item)
    },
    get_more_data: function (offset, type) {
      let self = this
      if (!self.model || !self.limit || !offset & offset !== 0 || !self.viewId || !self.domain) {
        return
      }
      self.$http.get('/odoo/mobile/get/list/view/data', {
        params: {
          model: self.model,
          viewId: self.viewId,
          domain: JSON.stringify(self.domain),
          limit: self.limit,
          order: self.order,
          offset: offset
        }
      }).then(function (response) {
        if (!response) {
          return ''
        }
        if (type === 'add') {
          if (response.length !== self.offset_step) {
            self.is_all_records_data = true
          } else {
            self.is_all_records_data = false
          }
          self.now_record_length = response.length
          self.list = self.list.concat(response)
        } else {
          self.now_record_length = response.length
          self.list = response
        }
      }).catch(function (error) {
        console.error('OdooTree数据失败', error, __filename)
      })
    },
    refresh: function (done) {
      let self = this
      setTimeout(function () {
        self.offset = 0
        self.list = []
        self.is_all_records_data = false
        self.$nextTick(() => {
          self.get_more_data(0, 'fresh')
        })
        done()
      }, 3000)
    },
    infinite: function (done) {
      let self = this
      if (self.is_all_records_data || self.now_record_length < self.offset_step) {
        setTimeout(function () {
          done(true)
          self.is_all_records_data = false
        }, 3000)
        return
      }
      setTimeout(function () {
        self.offset = Number(self.offset) + Number(self.offset_step)
        self.$nextTick(() => {
          self.get_more_data(self.offset, 'add')
        })
        done()
      }, 3000)
    },
    actionSheetFunction: function (itemIndex, items) {
      let self = this
      if (itemIndex === 0) {
        self.$router.push({ name: 'NewForm', params: { model: self.model } })
      }
    }
  }
}
</script>

<style >

</style>
